package com.oazon.moco.playlist.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.oazon.moco.playlist.bean.ItemType;
import com.oazon.moco.playlist.bean.MediaCollectionBean;

public class MediaCollectionDao {

	private SQLiteDatabase db;

	private static final String TABLE_NAME = "media_collection";

	private static final String ID = "id";
	private static final String ITEM_TYPE = "item_type";
	private static final String TITLE = "title";
	private static final String ARTIST = "artist";
	private static final String ARTIST_ID = "artist_id";
	private static final String CAN_SCROLL = "can_scroll";
	private static final String CAN_PLAY = "can_play";
	private static final String CAN_ENUMERATE = "can_enumerate";
	private static final String CAN_ADD_TO_FAVORITES = "can_add_to_favorites";
	private static final String CAN_CACHE = "can_cache";
	private static final String CAN_SKIP = "can_skip";
	private static final String ALBUM_ART_URI = "album_art_uri";
	private static final String AUTH_REQUIRED = "auth_required";
	private static final String HOMOGENEOUS = "homogeneous";
	private static final String CAN_ADD_TO_FAVORITE = "can_add_to_favorite";
	private static final String READ_ONLY = "read_only";
	private static final String CREAT_TIME = "creat_time";
	private static final String LIFECYCLE = "lifecycle";
	private static final String PARENT_ID = "parent_id";
	private static final String PROVIDER = "provider";

	public MediaCollectionDao(Context context) {
		MocoDB resourceDB = new MocoDB(context);
		db = resourceDB.getReadableDatabase();
	}

	public synchronized long insertIntoMediaCollection(MediaCollectionBean bean) {
		if (bean == null) {
			return -1l;
		}
		String time = new Date().toString();
		bean.setCreatTime(time);

		ContentValues values = new ContentValues();
		if (bean.getId() == null)
			return -1;

		values.put(ID, bean.getId());
		if (bean.getItemType() != null) {
			values.put(ITEM_TYPE, bean.getItemType().value());
		}
		values.put(TITLE, bean.getTitle());
		values.put(ARTIST, bean.getArtist());
		values.put(ARTIST_ID, bean.getArtistId());
		values.put(CAN_SCROLL, bean.getCanScroll());
		values.put(CAN_PLAY, bean.getCanPlay());
		values.put(CAN_ENUMERATE, bean.getCanEnumerate());
		values.put(CAN_ADD_TO_FAVORITES, bean.getCanAddToFavorites());
		values.put(CAN_CACHE, bean.getCanCache());
		values.put(CAN_SKIP, bean.getCanSkip());
		values.put(ALBUM_ART_URI, bean.getAlbumArtURI());
		values.put(AUTH_REQUIRED, bean.getAuthRequired());
		values.put(HOMOGENEOUS, bean.getHomogeneous());
		values.put(CAN_ADD_TO_FAVORITE, bean.getCanAddToFavorite());
		values.put(READ_ONLY, bean.getReadOnly());
		values.put(CREAT_TIME, bean.getCreatTime());
		values.put(LIFECYCLE, bean.getLifecycle());
		values.put(PARENT_ID, bean.getParentId());
		values.put(PROVIDER, bean.getProvider());

		return db.insert(TABLE_NAME, "", values);

	}

	public synchronized long insertListMediaCollection(
			List<MediaCollectionBean> collectionBeans) {
		android.util.Log.d("wowdd1", "insertListMediaCollection");
		long size = 0l;
		if (collectionBeans == null || collectionBeans.size() == 0) {
			return -1l;
		}
		for (MediaCollectionBean mediaCollectionBean : collectionBeans) {
			try {
				insertIntoMediaCollection(mediaCollectionBean);
			} catch (Exception e) {
				e.printStackTrace();
			}

			size++;
		}
		android.util.Log.d("wowdd1", "insertListMediaCollection "
				+ collectionBeans.size());
		return size;
	}

	public List<MediaCollectionBean> selectByParentId(String parentId,
			String provider) {

		Cursor cursor = db.query(TABLE_NAME, null, PARENT_ID + "=? and "
				+ PROVIDER + "=?", new String[] { parentId, provider }, null,
				null, null);
		List<MediaCollectionBean> collectionBeans = new ArrayList<MediaCollectionBean>();
		MediaCollectionBean mediaCollectionBean = null;

		while (cursor.moveToNext()) {
			mediaCollectionBean = new MediaCollectionBean();
			mediaCollectionBean.setId(cursor.getString(cursor
					.getColumnIndex(ID)));
			mediaCollectionBean.setAlbumArtURI(cursor.getString(cursor
					.getColumnIndex(ALBUM_ART_URI)));
			mediaCollectionBean.setArtist(cursor.getString(cursor
					.getColumnIndex(ARTIST)));
			mediaCollectionBean.setArtistId(cursor.getString(cursor
					.getColumnIndex(ARTIST_ID)));
			mediaCollectionBean.setAuthRequired(Boolean.valueOf(cursor
					.getString(cursor.getColumnIndex(AUTH_REQUIRED))));
			mediaCollectionBean.setCanAddToFavorite(Boolean.valueOf(cursor
					.getString(cursor.getColumnIndex(CAN_ADD_TO_FAVORITE))));
			mediaCollectionBean.setCanAddToFavorites(Boolean.valueOf(cursor
					.getString(cursor.getColumnIndex(CAN_ADD_TO_FAVORITES))));
			mediaCollectionBean.setCanCache(Boolean.valueOf(cursor
					.getString(cursor.getColumnIndex(CAN_CACHE))));
			mediaCollectionBean.setCanEnumerate(Boolean.valueOf(cursor
					.getString(cursor.getColumnIndex(CAN_ENUMERATE))));
			mediaCollectionBean.setCanPlay(Boolean.valueOf(cursor
					.getString(cursor.getColumnIndex(CAN_PLAY))));
			mediaCollectionBean.setCanScroll(Boolean.valueOf(cursor
					.getString(cursor.getColumnIndex(CAN_SCROLL))));
			mediaCollectionBean.setCanSkip(Boolean.valueOf(cursor
					.getString(cursor.getColumnIndex(CAN_SKIP))));
			mediaCollectionBean.setCreatTime(cursor.getString(cursor
					.getColumnIndex(CREAT_TIME)));
			mediaCollectionBean.setHomogeneous(Boolean.valueOf(cursor
					.getString(cursor.getColumnIndex(HOMOGENEOUS))));
			mediaCollectionBean.setItemType(ItemType.fromValue(cursor
					.getString(cursor.getColumnIndex(ITEM_TYPE))));
			mediaCollectionBean.setLifecycle(cursor.getString(cursor
					.getColumnIndex(LIFECYCLE)));
			mediaCollectionBean.setReadOnly(Boolean.valueOf(cursor
					.getString(cursor.getColumnIndex(READ_ONLY))));
			mediaCollectionBean.setTitle(cursor.getString(cursor
					.getColumnIndex(TITLE)));
			mediaCollectionBean.setParentId(cursor.getString(cursor
					.getColumnIndex(PARENT_ID)));
			mediaCollectionBean.setProvider(cursor.getString(cursor
					.getColumnIndex(PROVIDER)));

			collectionBeans.add(mediaCollectionBean);
		}
		return collectionBeans;
	}

	public synchronized int deleteByParentId(String parentId, String provider) {
		if (parentId == null) {
			return -1;
		}
		if (provider == null) {
			provider = "";
		}
		return db.delete(TABLE_NAME, PARENT_ID + "=? and " + PROVIDER + "=?",
				new String[] { parentId, provider });
	}

	public synchronized int deleteByProvider(String provider) {
		if (provider == null) {
			provider = "";
		}
		return db
				.delete(TABLE_NAME, PROVIDER + "=?", new String[] { provider });
	}
}
